var _THIS;
var ERR_OK = 200;
var DEFAUT_INDEX = top._CTX + "/web/currentdata/index";
var MENUS = [{
    children: [
        {
            children: [
                {
                    children: [],
                    icon: 'icon-third-small-jiance',
                    id: '110',
                    name: '实时数据',
                    parent: '11',
                    path: top._CTX + '/web/currentdata/index'
                }, {
                    children: [],
                    icon: 'icon-third-small-shebei1',
                    id: '111',
                    name: '设备状态',
                    parent: '11',
                    path: top._CTX + '/web/device/statusIndex'
                }
            ],
            icon: 'icon-third-small-shuju',
            id: '11',
            name: '数据监控',
            parent: '1',
            path: 'asset'
        }, {
            children: [
                {
                    children: [],
                    icon: 'icon-third-small-zhandian1',
                    id: '120',
                    name: '站点管理',
                    parent: '12',
                    path: top._CTX + '/web/spot/index'
                }, {
                    children: [],
                    icon: 'icon-third-small-shebei',
                    id: '121',
                    name: '设备管理',
                    parent: '12',
                    path: top._CTX + '/web/device/index'
                }, {
                    children: [],
                    icon: 'icon-third-small-tongdao2',
                    id: '122',
                    name: '通道管理',
                    parent: '12',
                    path: top._CTX + '/web/channel/index'
                }, {
                    children: [],
                    icon: 'icon-third-small-tongdao1',
                    id: '123',
                    name: '虚拟通道',
                    parent: '12',
                    path: top._CTX + '/web/virtual/index'
                }
            ],
            icon: 'icon-third-small-peizhi',
            id: '12',
            name: '基础配置',
            parent: '1',
            path: 'peizhi'
        }, {
            children: [
                {
                    children: [],
                    icon: 'icon-third-small-net',
                    id: '130',
                    name: '网关配置',
                    parent: '13',
                    path: top._CTX + '/web/net/index'
                }, {
                    children: [],
                    icon: 'icon-third-small-xieyi1',
                    id: '131',
                    name: '协议库',
                    parent: '13',
                    path: top._CTX + '/web/protocol/index'
                }, {
                    children: [],
                    icon: 'icon-third-small-shuchu',
                    id: '132',
                    name: '输出设置',
                    parent: '13',
                    path: top._CTX + '/web/sendto/index'
                }, {
                    children: [],
                    icon: 'el-icon-document-remove',
                    id: '133',
                    name: '基础信息',
                    parent: '13',
                    path: top._CTX + '/web/settings/index'
                }, {
                    children: [],
                    icon: 'el-icon-unlock',
                    id: '134',
                    name: '智件注册',
                    parent: '13',
                    path: top._CTX + '/web/reg/index'
                }, {
                    children: [],
                    icon: 'el-icon-user',
                    id: '135',
                    name: '用户管理',
                    parent: '13',
                    path: top._CTX + '/web/user/index'
                }
            ],
            icon: 'el-icon-setting',
            id: '13',
            name: '系统设置',
            parent: '1',
            path: 'set'
        }
    ],
    icon: 'inbox',
    id: '1',
    name: '智联',
    parent: '0',
    path: 'asset'
}]

function getContextPath() {
    var pathName = document.location.pathname;
    var index = pathName.substr(1).indexOf("/");
    var result = pathName.substr(0, index + 1);
    return result;
}

function getTopUrl() {
    const href = parent.window.location.href
    const pathname = parent.window.location.pathname
    const pathIndex = href.indexOf(pathname)
    const topUrl = href.substring(0, pathIndex)
    return topUrl
}

var app = new Vue({
    el: '#app',
    data: function () {
        return {
            tabs: {list: [{path: DEFAUT_INDEX, name: '实时数据'}]},
            time: '',
            router: {
                curRouter: {
                    header: 'asset',
                    menu: '智联',
                    item: DEFAUT_INDEX
                },
                leftmenu: {menu_flag: true},
                menus: []
            },
            subMenuIndexs: [],
//    	  service:managerService,
            modifyInfo: {
                formName: "modifyInfoForm",
                rules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                    ]
                },
                visible: false,
                data: {
                    name: '',
                    phone: ''
                }
            },
            manager: {
                name: '',
                phone: ''
            },
            timeout: 540000,
            serverTimeoutObj: {},
            iconUrl: top._CTX + "/images/logo-sm.png"
        };
    },
    mounted: function () {
        this.manager.name = this.modifyInfo.data.name = top.name;
        this.manager.phone = this.modifyInfo.data.phone = top.phone;
        this.getMenuList();
        var _this = this;
        var dateTime = new Date();
        dateTime.setSeconds(dateTime.getSeconds() + 1);
        setInterval(function () {
            dateTime.setSeconds(dateTime.getSeconds() + 1);
            _this.time = formatDate(dateTime, "yyyy/MM/dd hh:mm:ss");
        }, 1000);
        this.getIconUrl();
    },
    methods: {
        getByClass: function (oParent, sClass) {
            var aResult = {};
            var aEle = oParent.getElementsByTagName('*');

            for (var i = 0; i < aEle.length; i++) {
                if (aEle[i].className == sClass) {
                    aResult = aEle[i];
                    return aResult;
                }
            }
            return aResult;
        },
        addTab: function (menu) {
            var tabs = this.tabs.list;
            var has = false;
            var _this = this;
            tabs.forEach(function (tab, index) {
                if (tab.path === menu.path) {
                    _this.router.curRouter.item = tab.path;
                    has = true;
                }
            });
            if (!has) {
                this.tabs.list.push(menu);
                _this.router.curRouter.item = menu.path;
            }
        },
        removeTab: function (targetName) {
            if (targetName == DEFAUT_INDEX) return;
            var tabs = this.tabs.list;
            var activeName = this.router.curRouter.item;
            var name = "";
            if (activeName === targetName) {
                tabs.forEach(function (tab, index) {
                    if (tab.path === targetName) {
                        var nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.path;
                            name = nextTab.name;
                        }
                    }
                });
            }
            this.router.curRouter.item = activeName;
            this.tabs.list = tabs.filter(function (tab) {
                return tab.path !== targetName;
            });
        },
        menuTo: function (index) {
            var _this = this;
            this.router.menus.forEach(function (menu) {
                if (menu.path === index) {
                    _this.router.curRouter.header = menu.path;
                    return;
                }
            });
        },
        changeMenuStyle: function () {
            this.router.leftmenu.menu_flag = this.router.leftmenu.menu_flag ? false : true;
        },
        onTabClick: function (Tabs) {
            var index = this.tabs.list.indexOf(Tabs.path);
            if (index !== -1) {
                this.router.curRouter.item = this.tabs.list[index].path;
            }
        },
        // 退出登录
        logout: function () {
            this.$confirm('确认退出吗?', '提示', {
                type: 'warning'
            }).then(function () {
                window.location.href = top._CTX + '/logout';
            });
        },
        getMenuList: function () {
            var _this = this;
            this.router.menus = MENUS;
//			 this.router.menus[0].children[0].children=menuArray;
            this.router.curRouter.header = MENUS[0].path;
            this.router.curRouter.item = DEFAUT_INDEX;

            //展开菜单
            this.router.menus.forEach(function (menu) {
                menu.children.forEach(function (child) {
                    _this.subMenuIndexs.push(child.path);
                });
            });
        },
        redirect: function (name, path) {
            this.addTab({name: name, path: path});
        },
        modifyInfo: function (managerId) {
            var menu = {
                children: [],
                icon: 'el-icon-third-yly_guanliyuan',
                id: '110',
                name: '修改资料',
                parent: '11',
                path: top._CTX + "/manager/info?backUrl=&id=" + managerId
            }
            this.addTab(menu);
        },
        showSuccessMsg: function (msg) {
            this.$message({
                message: msg,
                type: 'success'
            });
        },
        showFailMsg: function (msg) {
            this.$message({
                message: msg,
                type: 'fail'
            });
        },
        getIconUrl: function () {
            const _this = this;
            _HTTP.post("/api/settings/getSettings", ['logo'], {
                success(res) {
                    // console.log(res)
                    if (res != null && res.length > 0 && res[0].value != null) {
                        _this.iconUrl = top._CTX + res[0].value;
                        // console.log(_this.iconUrl)
                    }
                },
                fail(res) {
                    // console.log(res)
                }
            })
        }
    }
});
top.indexApp = app;
